package com.group.controller;

import com.base.view.GeneralResult;
import com.base.view.ListResult;
import com.google.common.base.Function;
import com.group.dao.LotteryDao;
import com.group.model.ActUser;
import com.group.model.In.LotteryIn;
import com.group.model.LotteryPrize;
import com.group.model.SystemPara;
import com.group.model.WinningRecord;
import com.group.service.LotteryService;
import com.group.util.DateUtil;
import com.group.util.NoteException;
import com.sun.org.apache.xerces.internal.impl.xpath.XPath;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 抽奖接口
 * Cheney
 */
@RequestMapping(value = "/lottery")
@RestController
public class LotteryController extends BaseController{

    @Autowired
    LotteryService lotteryService;

    @Autowired
    LotteryDao lotteryDao;

    /**
     * 抽奖
     * @param in
     * @return
     */
    @RequestMapping(value = "/doLottery", method = {RequestMethod.POST})
    public GeneralResult doLottery(
            @RequestBody LotteryIn in){
        System.out.println("--------------------------------"+in.getUserId()+"-----------"+in.getMultiple()+"-----------------------------------");
        LotteryPrize lp = lotteryService.doLottery(in.getUserId(), in.getMultiple());
        return new GeneralResult().setValue(lp);
    }

    /**
     * 获得喇叭
     * @param limit
     * @return
     */
    @RequestMapping(value = "/gethorn", method = {RequestMethod.GET})
    public ListResult doWhat(Integer limit){
        List<String> list = lotteryService.getHorn(limit);
        return new ListResult().setList(list).setTotal(limit);
    }

    /**
     * 获取奖品列表
     * @return
     */
    @RequestMapping(value = "/getPrizeList", method = {RequestMethod.GET})
    public ListResult getPrizeList(){
        List<LotteryPrize> list = lotteryDao.findAll();
        return new ListResult().setList(list);
    }

    /**
     * 获取次数
     * @param userId
     * @return
     */
    @RequestMapping(value = "/getTimes", method = {RequestMethod.GET})
    public GeneralResult getTimes(Long userId){
        ActUser actUser = beanDao.quickQueryOne(ActUser.class, "userId", userId);
        return new GeneralResult().setValue(actUser);
    }

    /**
     * 分享加次数
     * @param userId
     * @return
     */
    @RequestMapping(value = "/shareTimes", method = {RequestMethod.GET})
    public GeneralResult shareInc(Long userId){
        ActUser actUser = beanDao.quickQueryOne(ActUser.class, "userId", userId);
        Function<String, Integer> f = Integer::parseInt;
        int i = f.apply(beanDao.quickQueryOne(SystemPara.class, "name", "shareTimes").getValue());
        if(actUser.getShareTimes() >= i){
            return new GeneralResult().setValue(actUser);
        }
        actUser.setShareTimes(actUser.getShareTimes() + 1);
        actUser.setTimes(actUser.getTimes() + 1);
        beanDao.save(actUser);
        return new GeneralResult().setValue(actUser);
    }

    /**
     * 获取个人中奖纪录
     * @param userId
     * @return
     */
    @RequestMapping(value = "/getUserRecord", method = {RequestMethod.GET})
    public ListResult getUserRecord(Long userId, Integer limit){
        List<WinningRecord> list = lotteryDao.getUserRecord(userId, limit);
        return new ListResult().setList(list);
    }

    @RequestMapping(value = "/TimerStart", method = {RequestMethod.GET})
    public void TimerStart(){
        lotteryService.updatelt();
    }
}
